Accessibility

DRK Volume 4 Article

The XML Indenter Component

What Is the XML Indenter Component?

When XML is generated automatically, either through ColdFusion's XML API or tags like cfsavecontent, it is not easily readable, even though it may be well formed. The XML Indenter component transforms XML objects or strings into nicely indented, human-readable formats.

Installing the Xml Indenter Component

The advantage of XML2Query over WDDX is that the structure that XML2Query expects the XML to be in is much more human-readable than WDDX, which means it's ideal for storing application configuration information and other data that needs to be maintained later by hand.

Installing the XML2Query Component

The XML Indenter component is contained in the xml package, so the easiest way to install it is to create the directory structure com/macromedia/xml in your web root.

For instance, if your web root is wwwroot, then xml_indenter.cfc should be here:

wwwroot/com/macromedia/xml/xml_indenter.cfc

You can also place the component in a directory under a ColdFusion mapping or in a custom tag root.

Using the XML Indenter

Once you've properly installed the XML Indenter component, the best way to use it is to create an instance using the cfobject tag, like this:

<cfobject component="com.macromedia.xml.xml_indenter" name="indenter"/>

You can set either an XML string or an XML object on an XML Indenter instance using the setXmlObject or setXmlString method, like this:

<cfinvoke component="#indenter#" method="setXmlObject" xmlObject="#xmlObj#"/>

or like this:

<cfinvoke component="#indenter#" method="setXmlString" xmlObject="#xmlStr#"/>

The XML Indenter has several properties you can set in order to configure its behavior. Below is a table containing descriptions of all the XML Indenter's functions:

Table 1. Properties of the XML Indenter Component


Function Description
setXmlObject Sets the XML object you want to indent. You can either use this function to set an XML object or use setXmlString to set the XML as a string.
setXmlString Sets the XML you want to indent. You can either use this function to set the XML or use setXmlObject to set the XML as an XML object.
setXmlEncoding Sets the encoding for the XML document. This is the value of the "encoding" attribute inside the XML declaration.
setUseTabs Tells xml_indenter to indent the document with tabs rather than spaces. This is the default behavior, so if you want tabs you do not need to call this function.
setUseSpaces Tells xml_indenter to indent the document with spaces rather than tabs.
setSteps Sets the number of steps from the left margin to indent. The default is one (since the default step type is tabs).
setUseDoubleQuotes Tells xml_indenter to use double quotes for attributes. This is the default.
setUseSingleQuotes Tells xml_indenter to use single quotes for attributes.
getIndentedXml StringReturns the indented XML as a string.
getIndentedXmlObject Returns the indented XML as an XML object.

Once you have set either your XML string or XML object on an instance of the XML Indenter component, and after you have configured the XML Indenter using some of the functions above, call getIndentedXmlString or getIndentedXmlObject to have your well-formed, properly indented XML returned to you.

How Does the XML Indenter Work?

Because it would not be practical to attempt to parse the specified XML "by hand" (meaning trying to pick apart the XML tags, attributes, and character data using ColdFusion string functions), the XML Indenter component actually uses the built-in XML support of ColdFusion MX to parse the document before traversing the XML object and rebuilding the XML document from scratch. Because the XML Indenter component rebuilds the entire XML document, it ensures a clean, consistent, and human-readable format for any type of XML document.